﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace DocAddIn
{
    [RunInstaller(true)]
    public partial class DocAddInInstaller : Installer
    {
        public DocAddInInstaller()
        {
            InitializeComponent();
        }

        public override void Install(System.Collections.IDictionary statesaver)
        {
            DirectoryInfo addInDirOrigem;
            DirectoryInfo addInDirDestino;
            FileInfo addInFileOrigem;
            FileInfo addInFileDestino;
            StringBuilder aux;
            XmlDocument doc;
            string assembly;

            base.Install(statesaver);

            try
            {
                assembly = this.Context.Parameters["assemblypath"];
                addInDirOrigem = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(assembly),
                                                   "Addins"));
                addInFileOrigem = new FileInfo(Path.Combine(addInDirOrigem.FullName, "DocAddIn.AddIn"));

                aux = new StringBuilder();
                Classes.DllImport.SHGetSpecialFolderPath(IntPtr.Zero, aux, Classes.DllImport.CSIDL_COMMON_APPDATA, false);

                addInDirDestino = new DirectoryInfo(Path.Combine(aux.ToString(),
                                                                 "Microsoft\\VisualStudio\\9.0\\Addins"));
                addInFileDestino = new FileInfo(Path.Combine(addInDirDestino.FullName, "DocAddIn.AddIn"));

                if (!addInDirDestino.Exists)
                {
                    Directory.Move(addInDirOrigem.FullName, addInDirDestino.FullName);
                }
                else
                {
                    File.Move(addInFileOrigem.FullName, addInFileDestino.FullName);
                    addInDirOrigem.Delete(true);
                }

                doc = new XmlDocument();
                doc.Load(addInFileDestino.FullName);

                doc["Extensibility"]["Addin"]["Assembly"].InnerText = assembly;
                doc["Extensibility"]["Addin"]["LoadBehavior"].InnerText = "1";
                doc["Extensibility"]["Addin"]["CommandPreload"].InnerText = "1";

                doc.Save(addInFileDestino.FullName);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            DirectoryInfo addInDirDestino;
            FileInfo addInFileDestino;
            StringBuilder aux;

            base.Uninstall(savedState);

            try
            {
                aux = new StringBuilder();
                Classes.DllImport.SHGetSpecialFolderPath(IntPtr.Zero, aux, Classes.DllImport.CSIDL_COMMON_APPDATA, false);

                addInDirDestino = new DirectoryInfo(Path.Combine(aux.ToString(),
                                                                 "Microsoft\\VisualStudio\\9.0\\Addins"));

                addInFileDestino = new FileInfo(Path.Combine(addInDirDestino.FullName, "DocAddIn.AddIn"));

                addInFileDestino.Delete();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
